/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	liuyang 
 * 
 * <p>描   述  :   NumberManager.java for com.pengtu.service    </p>
 * 
 * <p>最后修改 : $: 2011-8-5-下午1:37:08 v 1.0.0	 liuyang   $     </p>
 * 
 */

package com.pengtu.service;

import java.util.Date;
import java.util.Iterator;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.dao.app.SerialNumberDao;
import com.pengtu.dao.app.TrialNumberDao;
import com.pengtu.entity.app.SerialNumber;
import com.pengtu.entity.app.TrialNumber;
import com.pengtu.utils.DateUtils;

/**
 * 
 * NumberManager
 * 
 * 2011-8-5 下午1:37:08
 * 
 * @version 1.0.0
 * 
 */
@Component
@Transactional
public class NumberManager {
	

	private OracleSequenceMaxValueIncrementer maxValueIncrementer;

    @Autowired
	public void setMaxValueIncrementer(
			OracleSequenceMaxValueIncrementer maxValueIncrementer) {
		this.maxValueIncrementer = maxValueIncrementer;
	}

	/**
	 * 随机数
	 * getRandomNum:
	 * 适用:
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public String getRandomNum(){
		maxValueIncrementer.setIncrementerName("HIBERNATE_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		return maxValueIncrementer.nextStringValue();
//		Random r = new Random();
//		int t1 = (int) (r.nextDouble() * 10);
//		int t2 = (int) (r.nextDouble() * 10);
//		int t3 = (int) (r.nextDouble() * 10);
//		if (t1 == t2) {
//			t2 = (int) (r.nextDouble() * 10);
//		}
//		if (t1 == t3) {
//			t3 = (int) (r.nextDouble() * 10);
//		}
//		String t4 = t1 + "";
//		t4 += t2;
//		t4 += t3;
//		return t4;
	}
	
	/**
	 * 质量监督业务受理编号 getApplNumber: 适用:
	 * 
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public String getProcessQualityNumber() { 
		StringBuffer sBuffer = new StringBuffer("质量监督(公或水) [");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("] ");
		maxValueIncrementer.setIncrementerName("QUALITY_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append(" 号");
		return sBuffer.toString();
	}
	
	/**
	 * 质量监督业务申请编号
	 * getApplyQualityNumber:
	 * 适用:
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public String getApplyQualityNumber(){
		StringBuffer sBuffer = new StringBuffer("公路(水运)申请  [");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("] ");
		maxValueIncrementer.setIncrementerName("APPLY_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append(" 号");
		return sBuffer.toString();
	}
	
	/**
	 * 得到竣工申请编号
	 * getApplyCompleNumber:
	 * 适用:
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public String getApplyCompleNumber(){
		StringBuffer sBuffer = new StringBuffer("公路(水运)竣工 [");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("] ");
		maxValueIncrementer.setIncrementerName("COMPLE_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append(" 号");
		return sBuffer.toString();
	}
	
	/**
	 * 得到实验室授权申请编号
	 * getApplyLabsNumber:
	 * 适用:
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public String getApplyLabsNumber() {
		StringBuffer sBuffer = new StringBuffer("实验室授权申请 [");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("] ");
		maxValueIncrementer.setIncrementerName("LABS_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append(" 号");
		return sBuffer.toString();
	}
	
	/**
	 * 得到交工检测申请编号
	 * getApplyDetnNumber:
	 * 适用:
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public String getApplyDetnNumber() {
		StringBuffer sBuffer = new StringBuffer("公路(水运)交工 [");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("] ");
		maxValueIncrementer.setIncrementerName("DETN_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append(" 号");
		return sBuffer.toString();
	}
	
	/**
	 * 得到交工检测报告申请编号
	 * getApplyAcctNumber:
	 * 适用:
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public String getApplyAcctNumber() {
		StringBuffer sBuffer = new StringBuffer("公路(水运)交报 [");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("] ");
		maxValueIncrementer.setIncrementerName("ACCT_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append(" 号");
		return sBuffer.toString();
	}
	/**
	 * 
	 * getprojCode:
	 * 适用:添加工程编号
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public String getprojCode(){
		StringBuffer sBuffer = new StringBuffer("DGPRJ-");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("-");
		maxValueIncrementer.setIncrementerName("PROJECT_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append("");
		return sBuffer.toString();
	}
	public String getApplyCostBNumber() {
		StringBuffer sBuffer = new StringBuffer("初步设计概算申请 [");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("] ");
		maxValueIncrementer.setIncrementerName("COST_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append(" 号");
		return sBuffer.toString();
	}
	public String getApplyCostCNumber() {
		StringBuffer sBuffer = new StringBuffer("竣工决算审查 [");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("] ");
		maxValueIncrementer.setIncrementerName("COST_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append(" 号");
		return sBuffer.toString();
	}
	public String getApplyCostDNumber() {
		StringBuffer sBuffer = new StringBuffer("施工图预算审查 [");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("] ");
		maxValueIncrementer.setIncrementerName("COST_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append(" 号");
		return sBuffer.toString();
	}
	public String getApplyCostENumber() {
		StringBuffer sBuffer = new StringBuffer("投资估算审查 [");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("] ");
		maxValueIncrementer.setIncrementerName("COST_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append(" 号");
		return sBuffer.toString();
	}
	public String getApplyCostLNumber() {
		StringBuffer sBuffer = new StringBuffer("最高限价审查 [");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("] ");
		maxValueIncrementer.setIncrementerName("COST_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append(" 号");
		return sBuffer.toString();
	}
	
	public String getApplyCostMNumber() {
		StringBuffer sBuffer = new StringBuffer("设计变更预算审查 [");
		sBuffer.append(DateUtils.getCurrentYear());
		sBuffer.append("] ");
		maxValueIncrementer.setIncrementerName("COST_SEQUENCE");
		maxValueIncrementer.setPaddingLength(3);
		sBuffer.append(maxValueIncrementer.nextStringValue()).append(" 号");
		return sBuffer.toString();
	}
	
	/*
     *
	 * 根据实验类型产生报告编号最大序列号, 试样编号最大序列号
     * 报告编号 = 编号类型  + 编号模式 + “-” + 最大序列号
     * 编号模式：取当前时间yyyyMMdd
     * 编号类型：wt0~wt9
     * 最大序列号为Oracle Sequence自动增加
	
	
	public String getReportLargest(){
		StringBuffer sBuffer = new StringBuffer();
		//sBuffer.append("-");
		sBuffer.append(this.model(new Date()));
		sBuffer.append("-");
		maxValueIncrementer.setIncrementerName("REPORTLARGEST");
		maxValueIncrementer.setPaddingLength(4);
		sBuffer.append(maxValueIncrementer.nextStringValue());
		return sBuffer.toString();
	}
	public String getSampleLargest(){
		StringBuffer sBuffer = new StringBuffer();
		//sBuffer.append("-");
		sBuffer.append(this.model(new Date()));
		sBuffer.append("-");
		maxValueIncrementer.setIncrementerName("SAMPLELARGEST");
		maxValueIncrementer.setPaddingLength(4);
		sBuffer.append(maxValueIncrementer.nextStringValue());
		return sBuffer.toString();
	}
	
	com.pengtu.dao.app.TrialNumberDao trialNumberDao;
	public String getReportCode(String str){
		if(str.equals("wt01")){
			return "WT-01" + this.getReportLargest();
	    }else if(str.equals("wt02")){
			return "WT-02" + this.getReportLargest(); 
		}else if(str.equals("wt03")){
			return "WT-03" + this.getReportLargest();
		}else if(str.equals("wt04")){
			return "WT-04" + this.getReportLargest(); 
		}else if(str.equals("wt05")){
			return "WT-05" + this.getReportLargest();
	    }else if(str.equals("wt06")){
			return "WT-06" + this.getReportLargest(); 
		}else if(str.equals("wt07")){
			return "WT-07" + this.getReportLargest();
		}else if(str.equals("wt08")){
			return "WT-08" + this.getReportLargest();
		}else if(str.equals("wt09")){
			return "WT-09" + this.getReportLargest();
		}
		return null;
	}
 
 
	*/
	
	/*
	 * A-2序列编号
	 * 
	 * 
	 */
	
	private SerialNumberDao serialNumberDao;

	@Autowired
	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}
	
	//增
	public void saveSerialNumber(SerialNumber serialNumber){
		serialNumberDao.save(serialNumber);
	}
	
	public void saveSerialNumberAll(Set<SerialNumber> ssn) {
		Iterator<SerialNumber> ls =  ssn.iterator();
		while(ls.hasNext()){
			SerialNumber sn = new SerialNumber();
			sn = ls.next();
			serialNumberDao.save(sn);
		}
	}
	
	//删
	public void deleteSerialNumber(SerialNumber serialNumber){
		serialNumberDao.delete(serialNumber);
	}
	//通过类型删除
	public void deleteSerialNumberByType(String type){
		serialNumberDao.delete(this.getSerialNumber(type));
	}
	//通过ID删除
	public void deleteSerialNumber(String id){
		serialNumberDao.delete(id);
	}
	
	//改
	public void updateSerialNumber(SerialNumber serialNumber){
		serialNumberDao.update(serialNumber);
	}
	//通过type改
	public void updateSerialNumber(String type){
		serialNumberDao.update(this.getSerialNumber(type));
	}
	
	//查
	public SerialNumber getSerialNumber(String type){
		return serialNumberDao.getSerialNumberByType(type);
	}
	
	/*
	 * 模式约定：类型-日期-最大序列号
	 * 通过编号类型获取最大序列号
	 * 每次拿出最大序列号后自动加1
	 */

	public String getLargestByType(String type){
			
			//默认模式
			if(this.getSerialNumber(type).getModel()==null){
				this.getSerialNumber(type).setModel(this.getSerialNumber(type).getType() + "-" + "yyyyMMdd-XXXX");
				
			}
			String model = this.getSerialNumber(type).getModel();
			String t = model.split("-")[0];
			String d = model.split("-")[1];
			String l = model.split("-")[2];
			//默认最大序列号
			if(this.getSerialNumber(type).getLargest()==null||this.getSerialNumber(type).getLargest().trim().equals("")){
					this.getSerialNumber(type).setLargest(String.valueOf((int)Math.pow(10, (l.length()+d.length()))));
			}
			/**
			 * old上次记录日期
			 * now当前日期
			 */
			String old = String.valueOf(this.getSerialNumber(type).getLargest()).substring(0,d.length());
		    String now = DateUtils.convertDateToString(d, new Date());

		    if(!now.equals(old)){
		    	String s = "%0" + l.length() + "d";
		    	this.getSerialNumber(type).setLargest(now + String.format(s, 1));
		    }else{
		    	this.getSerialNumber(type).setLargest(String.valueOf(Long.parseLong(this.getSerialNumber(type).getLargest()) + 1));
		    }
		    	
		return this.getSerialNumber(type).getType() + "-" + this.getSerialNumber(type).getLargest();
	}
	

	
	/*
	 * A-3实验序列编号
	 * 
	 * 
	 */
	private TrialNumberDao trialNumberDao;

	@Autowired
	public void setTrialNumberDao(TrialNumberDao trialNumberDao) {
		this.trialNumberDao = trialNumberDao;
	}
	
	//增
	public void saveTrialNumber(TrialNumber trialNumber){
			trialNumberDao.save(trialNumber);
	}

	//通过实验类型查
	public TrialNumber getTrialNumber(String trialType){
			return trialNumberDao.getTrialTypeNumberByTrialType(trialType);
	}
	
	//通过实验类型删除
	public void deleteTrialNumberByTrialType(String trialType){
		trialNumberDao.delete(this.getTrialNumber(trialType));
	}
	
	
	public String[] getRandSLargest(String trialType){
		String[] rs = new String[100];
		

		//默认模式
		if(this.getTrialNumber(trialType).getReportModel()==null){
			this.getTrialNumber(trialType).setReportModel(this.getTrialNumber(trialType).getTrialType()
					.substring(0, 4) + this.getTrialNumber(trialType).getTrialType()
					.substring(6)  + "-" + "RyyMMdd-XXX");
			
		}
		if(this.getTrialNumber(trialType).getSampleModel()==null){
			this.getTrialNumber(trialType).setSampleModel(this.getTrialNumber(trialType).getTrialType()
					.substring(0, 4) + this.getTrialNumber(trialType).getTrialType()
					.substring(6)+ "-" + "SyyMMdd-XXX");
		}
		
		//报告编号
		String modelR = this.getTrialNumber(trialType).getReportModel();
		String tR = modelR.split("-")[0];
		String dR = modelR.split("-")[1];
		String lR = modelR.split("-")[2];
		//默认最大序列号
		if(this.getTrialNumber(trialType).getReportLargest()==null||this.getTrialNumber(trialType).getReportLargest().trim().equals("")){
				this.getTrialNumber(trialType).setReportLargest("R" + String.valueOf((int)Math.pow(10, (lR.length()+dR.length()-1))));
		}
		/**
		 * old上次记录日期
		 * now当前日期
		 */
		String oldR = this.getTrialNumber(trialType).getReportLargest().substring(1,(dR.length()));
	    String nowR = DateUtils.convertDateToString(dR.substring(1), new Date());
		String str1R = this.getTrialNumber(trialType).getReportLargest().substring(0,1);
	    if(!nowR.equals(oldR)){
	    	String s = "%0" + lR.length() + "d";
	    	this.getTrialNumber(trialType).setReportLargest(str1R + nowR + String.format(s, 1));
	    }else{
	    
	    	String str2R = this.getTrialNumber(trialType).getReportLargest().substring(1);
	    	this.getTrialNumber(trialType).setReportLargest(str1R + String.valueOf(Long.parseLong(str2R) + 1));
	    }
	    	
	    //试样编号
	    String modelS = this.getTrialNumber(trialType).getSampleModel();
		String tS = modelS.split("-")[0];
		String dS = modelS.split("-")[1];
		String lS = modelS.split("-")[2];
		//默认最大序列号
		if(this.getTrialNumber(trialType).getSampleLargest()==null||this.getTrialNumber(trialType).getSampleLargest().trim().equals("")){
			this.getTrialNumber(trialType).setSampleLargest("S" + String.valueOf((int)Math.pow(10, (lS.length()+dS.length()-1))));
	}
	    
		/**
		 * old上次记录日期
		 * now当前日期
		 */
		String oldS = this.getTrialNumber(trialType).getSampleLargest().substring(1,(dS.length()));
	    String nowS = DateUtils.convertDateToString(dS.substring(1), new Date());
		String str1S = this.getTrialNumber(trialType).getSampleLargest().substring(0,1);
	    if(!nowS.equals(oldS)){
	    	String s = "%0" + lS.length() + "d";
	    	this.getTrialNumber(trialType).setSampleLargest(str1S + nowS + String.format(s, 1));
	    }else{
	    
	    	String str2 = this.getTrialNumber(trialType).getSampleLargest().substring(1);
	    	this.getTrialNumber(trialType).setSampleLargest(str1S + String.valueOf(Long.parseLong(str2) + 1));
	    }
	    
	    	rs[0] = tR + "-" + this.getTrialNumber(trialType).getReportLargest(); 
			rs[1] = tS + "-" + this.getTrialNumber(trialType).getSampleLargest();
	
		return rs;
	}
}


